using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Visit.Dto;
using JTZY.Model.Visit;
using JTZY.Repository;
using JTZY.Service.Visit.IVisitService;
using JTZY.Model.Agent;
using JTZY.Model.Business.Dto;
using JTZY.Model.Business;

namespace JTZY.Service.Visit
{
    /// <summary>
    /// 用户访问记录Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IUservisitService), ServiceLifetime = LifeTime.Transient)]
    public class UservisitService : BaseService<Uservisit>, IUservisitService
    {
        /// <summary>
        /// 查询用户访问记录列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<UservisitDto> GetList(UservisitQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .LeftJoin<Corecmsuser>((it,user)=>it.UserId==user.Id)
                 .Where(predicate.ToExpression())
                .Select((it,user)=>new UservisitDto
                {
                    UserName = user.UserName,
                    CreateTime = it.CreateTime,
                    HasBuy = it.HasBuy,
                })
                .ToPage<UservisitDto>(parm);

            return response;
        }

        public UserVisitReportDto Statistics(UservisitQueryDto parm)
        {
            //UserVisitReportDto userVisitDto = new UserVisitReportDto();
            //userVisitDto.VisitCount = Queryable().Where(s => s.GoodUserId == parm.userId && s.CreateTime >= parm.dateTime && s.CreateTime < parm.dateTime.AddDays(1)).GroupBy(s => s.GoodUserId).Count();
            //userVisitDto.newUserCount = Context.Queryable<Corecmsuser>().Where(s => s.ParentId == parm.userId && s.CreateTime >= parm.dateTime && s.CreateTime < parm.dateTime.AddDays(1)).Count();
            //userVisitDto.user = Context.Queryable<Corecmsuser>().Where(it => it.Id == parm.userId).First();
            //userVisitDto.UserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.userId).Count();
            //userVisitDto.UnReUserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.userId && it.IsReviewed != 1).Count();
            //userVisitDto.ReUserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.userId && it.IsReviewed == 1).Count();
            //userVisitDto.OrderCount = Context.Queryable<Corecmsagentorder>().Where(s => s.UserId == parm.userId && s.CreateTime >= parm.dateTime && s.CreateTime < parm.dateTime.AddDays(1)).Count();
            //return userVisitDto;
            var user = Context.Queryable<Corecmsuser>().Where(it => it.Id == parm.UserId).First();
            var predicate = QueryExp(parm);
            var response=new UserVisitReportDto
            {
                AreaNames = user.AreaNames,
                UserName=user.UserName,
                AccountCode = user.AccountCode,
                VisitCount = Queryable().Where(s => s.ParentId == parm.UserId && s.CreateTime >= parm.CreateTime && s.CreateTime < parm.CreateTime.AddDays(1)).GroupBy(s => s.ParentId).Count(),
                UserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.UserId).Count(),
                newUserCount = Context.Queryable<Corecmsuser>().Where(s => s.ParentId == parm.UserId && s.CreateTime >= parm.CreateTime && s.CreateTime < parm.CreateTime.AddDays(1)).Count(),
                ReUserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.UserId && it.IsReviewed == 1).Count(),
                UnReUserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.UserId && it.IsReviewed != 1).Count(),
                OrderCount = Queryable().Where(s => s.ParentId == parm.UserId && s.CreateTime >= parm.CreateTime && s.CreateTime < parm.CreateTime.AddDays(1)).GroupBy(s => s.ParentId).Sum(s=>s.HasBuy)
            };

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Uservisit GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加用户访问记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Uservisit AddUservisit(Uservisit model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改用户访问记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateUservisit(Uservisit model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Uservisit> QueryExp(UservisitQueryDto parm)
        {
            var predicate = Expressionable.Create<Uservisit>();
            predicate= predicate.AndIF(parm.UserId.IsNotEmpty(), it => it.ParentId == parm.UserId);
            predicate = predicate.AndIF(parm.HasBuy.IsNotEmpty()&& parm.HasBuy==0, it => it.HasBuy ==0);
            predicate = predicate.AndIF(parm.HasBuy.IsNotEmpty() && parm.HasBuy == 1, it => it.HasBuy >=1);
            predicate = predicate.And(it=>it.CreateTime >= parm.CreateTime && it.CreateTime < parm.CreateTime.AddDays(1));
            return predicate;
        }
    }
}